package com.solution.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import com.solution.constant.LoginConstants;
import com.solution.constant.StatusConstants;
import com.solution.delegate.ExaminationServiceDelegate;
import com.solution.delegate.UserInfoServiceDelegate;
import com.solution.form.DashBoardForm;
import com.solution.form.ExamwiseData;
import com.solution.model.ExamType;
import com.solution.model.UserInfo;
import com.solution.util.SearchVO;



public class DashboardAction extends Action {
	Logger logger = Logger.getLogger(DashboardAction.class.getName());
    public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException { 
    	DashBoardForm loc_o_form = (DashBoardForm)form;
    	request.setAttribute("tab", "DASHBOARD");
    	if("/dashBoard".equals(mapping.getPath())){
			return dashBoard(mapping, loc_o_form, request,response);
		}	

        return mapping.findForward("success");
    }
	private ActionForward dashBoard(ActionMapping mapping, DashBoardForm loc_o_form, HttpServletRequest request, HttpServletResponse response) {
		logger.debug("Entering :: DashboardAction:dashBoard()");
		ActionMessages  errors  = new ActionMessages();
		HttpSession session = request.getSession();
		UserInfo userInfo = null;
		if(session != null){
			userInfo = (UserInfo)session.getAttribute(LoginConstants.SESSION_USER_INFO);
		}
		try {
			ExaminationServiceDelegate examService = new ExaminationServiceDelegate();
			UserInfoServiceDelegate userService = new UserInfoServiceDelegate();
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
			searchVO.addOrderDescending("createdOn");
			List<ExamType> examTypeList = examService.fetchExams(searchVO);
			List<ExamwiseData> examwiseDataList = new ArrayList<ExamwiseData>();
			ExamwiseData examwiseData = null;
			for(int i= 0;i<5 && examTypeList!=null && i<examTypeList.size() ;i++)
			{
				examwiseData = new ExamwiseData();
				searchVO.clear();
				searchVO.addEqualsCondition("examType.id", examTypeList.get(i).getId(),true);
				searchVO.addEqualsCondition("status", LoginConstants.USER_BLOCKED);
				searchVO.addIsNotNullCondition("examScore");
				searchVO.addOrderDescending("examScore");
				List<UserInfo> examineeList = userService.searchUser(searchVO);
				Double examineeScore= -1d;
				String examineeName = "";
				int count = 1;
				for(UserInfo user:examineeList){					
					if(examineeScore!= -1d && examineeScore == user.getExamScore() ){
						examineeScore = user.getExamScore();
						examineeName = user.getFirstName() +","+examineeName;
					}else {
						examineeName = user.getFirstName();
						examineeScore = user.getExamScore();
						if(count!=1){
							count++;
						}else{
							break;
						}
					}					
				}
				
				examwiseData.setExamineeName(examineeName);
				examwiseData.setExamName(examTypeList.get(i).getName());
				examwiseData.setExamID(examTypeList.get(i).getId());
				examwiseData.setExamType(examTypeList.get(i));
				examwiseData.setExamName(examTypeList.get(i).getName());
				examwiseData.setAttemptedDate(userInfo.getModifiedOn());
				examwiseData.setScore(examineeScore);
				if(!"".equals(examineeName)){
					examwiseDataList.add(examwiseData);
				}
				/*Total Exams*/
				searchVO.clear();
				loc_o_form.setClientType(userInfo.getClientType());
				searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(),true);
				searchVO.addEqualsCondition("status", (short)2);
				List<ExamType> examTypeListTemp = examService.fetchExams(searchVO);
				if(examTypeListTemp!=null){
					loc_o_form.setTotalExams(""+examTypeListTemp.size());
				}else{
					loc_o_form.setTotalExams("0");					
				}
				/*Total Examiner*/
				searchVO.clear();
				searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(),true);
				searchVO.addEqualsCondition("status",LoginConstants.USER_ACTIVE);
				searchVO.addIsNullCondition("examType");
				List<UserInfo> userInfoListTemp = userService.searchUser(searchVO);
				if(userInfoListTemp!= null){
					loc_o_form.setTotalExaminer(""+userInfoListTemp.size());
				}else{
					loc_o_form.setTotalExaminer("0");
				}
				/*Total Examinee*/
				searchVO.clear();
				searchVO.addEqualsCondition("clientType.id", userInfo.getClientType().getId(),true);
				searchVO.addEqualsCondition("status",LoginConstants.USER_ACTIVE);
				searchVO.addIsNotNullCondition("examType");
				userInfoListTemp = userService.searchUser(searchVO);
				if(userInfoListTemp!= null){
					loc_o_form.setTotalExaminee(""+userInfoListTemp.size());
				}else{
					loc_o_form.setTotalExaminee("0");
				}
								
				
			}
			/*Recent examinee*/
			searchVO.clear();
			//searchVO.addEqualsCondition("examType.id", examTypeList.get(i).getId(),true);
			searchVO.addEqualsCondition("status", LoginConstants.USER_BLOCKED);
			searchVO.addIsNotNullCondition("examScore");
			searchVO.addOrderDescending("createdOn");
			List<UserInfo> examineeListTemp = userService.searchUser(searchVO);
			loc_o_form.setRecentExamineeList(examineeListTemp);
			
			loc_o_form.setExamwiseDataList(examwiseDataList);
			
			
			
				
			
			} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapping.findForward("success");
	}
    
   
}
